<?php
namespace App\Repositories;

use App\Models\Cimo\UserNeed;
use App\Models\Cimo\UserResume;
use App\Models\Cimo\UserResumeTag;
use App\Models\Tags;
use App\User;
use DB;
use Illuminate\Database\Eloquent\SoftDeletes;

class UserNeedRepository
{
    use BaseRepository;
    use SoftDeletes;

    private $model;
    private $user;
    private $userResume;
    private $userResumeTag;
    private $tags;

    public function __construct(UserNeed $userNeed, User $user,
                                UserResume $userResume, Tags $tags,
                                UserResumeTag $userResumeTag)
    {
        $this->model = $userNeed;
        $this->user = $user;
        $this->userResume = $userResume;
        $this->userResumeTag = $userResumeTag;
        $this->tags = $tags;

    }

    public function getLists($userId, $status, $pageSize = 20)
    {
        if ($status > 0) {

            $needs = $this->model->whereHas('invites', function ($query) use ($status) {
                if ($status == 4) {
                    $query->where(['status' => $status, 'personal_comment' => 0])->where('interview_at', '>', 0)->orderBy('created_at', 'desc');
                } else {
                    $query->where('status', $status)->where('interview_at', '>', 0)->orderBy('created_at', 'desc');
                }
            })
                ->with('resume', 'user', 'invites')->where('user_id', $userId)
                ->orderBy('created_at', 'desc')->paginate($pageSize);
        } else {
            $needs = $this->model->with(['invites' => function ($query) use ($status) {
                $query->where('status', $status)->orderBy('created_at', 'desc');
            }])->with('resume', 'user')->where('user_id', $userId)
                ->orderBy('created_at', 'desc')->paginate($pageSize);
        }
        return $needs;
    }

    /**
     * 获取职位列表数据
     */
    public function getJobsList($skip, $pagesize, $where, $sort = 0)
    {
        $result = $this->model->select('user_resumes.name as resume_name', 'user_resumes.highest_degree', 'user_resumes.gznx',
            'user_resumes.phone', 'user_resumes.email', 'user_resumes.signature', 'user_resumes.intro',
            'user_needs.*', 'users.name as user_name', 'users.sex', 'users.signature')
            ->join('user_resumes', 'user_resumes.id', '=', 'user_needs.resume_id')
            /* $result = $this->model->select(DB::raw('qm_user_resumes.name as resume_name, qm_user_resumes.highest_degree, qm_user_resumes.gznx, qm_user_resumes.phone,
                 qm_user_resumes.email, qm_user_resumes.signature, qm_user_resumes.intro, qm_user_needs.*, qm_users.name as user_name, qm_users.sex,
                 qm_users.signature from (qm_user_needs INNER JOIN qm_user_resumes ON qm_user_resumes.id = qm_user_needs.resume_id) inner join qm_users on qm_users.id
                 = qm_user_needs.user_id'))*/
            ->join('users', 'users.id', '=', 'user_needs.user_id')
            ->where(function ($query) use ($where) {
                if (isset($where['areaid']) && $where['areaid'] > 0) {
                    $query->where('user_needs.city', '=', $where['areaid']);
                }
                if (isset($where['industry']) && $where['industry'] > 0) {
                    $query->where('user_needs.industry', '=', $where['industry']);
                }
                if ((isset($where['gznx']) && $where['gznx'] >= 0 && $where['gznx'] < 100) || is_array($where['gznx'])) {
                    if (is_array($where['gznx'])) {
                        $query->whereBetween('user_resumes.gznx', $where['gznx']);
                    } else {
                        $query->where('user_resumes.gznx', $where['gznx']);
                    }
                }
                if (isset($where['salary']) && $where['salary'] >= 0 && $where['salary'] < 100) {
                    $query->where('user_needs.expect_wages', $where['salary']);
                }
                if (isset($where['degree']) && $where['degree'] >= 0 && $where['degree'] < 100) {
                    $query->where('user_resumes.highest_degree', '=', $where['degree']);
                }
                if (isset($where['keyword']) && $where['keyword']) {
                    $query->where('user_needs.name', 'like', '%' . $where['keyword'] . '%');
                }

                switch (isset($where['sort']) && $where['sort']) {
                    case 1:
                        $query->orderBy('user_needs.updated_at', 'desc');
                        break;
                    default:
                        $query->orderBy('user_needs.created_at', 'desc');
                }

            })
            ->orderBy('user_needs.id', 'DESC')
            ->paginate($pagesize);
        //->skip($skip)->take($pagesize)->get();

        return $result;

    }

    public function getResumeTag($resume_id)
    {
        $tag_ids = $this->userResumeTag->where('resume_id', $resume_id)->lists('tag_id');
        return $this->tags->whereIn('tags_id', $tag_ids)->take(3)->lists('tags_name', 'tags_id');
    }


    public function getResumeByid($id)
    {
        return $this->userResume->withTrashed()->where('id', '=', $id)->first();
    }

    public function getUserByid($userid)
    {
        return $this->user->find($userid);
    }

    public function getUserResume($userid)
    {
        return $this->userResume->where('user_id', '=', $userid)->first();
    }

    public function getNeedsByResumeid($resume_id)
    {
        return $this->model->where('resume_id', '=', $resume_id)->first();
    }

    public function indexNeeds()
    {
        return $this->model->where('recommend', '=', '1')->get();
    }

    /**
     * 格式化数据
     *
     * @param array $input
     * @return number[]|array[]
     */
    public function formatData($input)
    {
        return $this->model->formart($input);
    }

    /**
     * store.
     *
     * @param array $input
     */
    public function store($input)
    {
        return $this->savePost($this->model, $input);
    }

    /**
     * 更新
     * @param int $id
     * @param $input
     * @return bool|\App\Models\AccountEvents
     */
    public function update($id, $input)
    {
        $model = $this->model->find($id);
        if (!$model)
            return FALSE;
        return $this->savePost($model, $input);
    }

    /**
     * save.
     *
     * @param Request $input 输入
     */
    public function savePost($events, $input)
    {
        $events->fill($input);
        $events->save();

        return $events;
    }

    /**
     * 根据主键+用户编号查找，校验用户权限
     * @param $id
     * @param $uid
     * @return mixed
     */
    public function findWithUid($id, $uid)
    {
        return $this->model->where('user_id', '=', $uid)->find($id);
    }
}

